package com.ljy.jymail.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.ljy.common.utils.R;
import com.ljy.jymail.ware.feign.MemberFeignService;
import com.ljy.jymail.ware.vo.FareVo;
import com.ljy.jymail.ware.vo.MemberAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljy.common.utils.PageUtils;
import com.ljy.common.utils.Query;

import com.ljy.jymail.ware.dao.WareInfoDao;
import com.ljy.jymail.ware.entity.WareInfoEntity;
import com.ljy.jymail.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {


    @Autowired
    MemberFeignService memberFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        String key =(String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key).or().like("name",key)
            .or().like("address",key)
            .or().like("areacode",key)
            ;
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }


    /*
    * @TODO 根据地址计算运费
    * @author ljy
    * @time 2022-02-07 08:22:17
    * @method getFare
    * @param param
    * @return java.math.BigDecimal
    */
    @Override
    public FareVo getFare(Long attrId) {

        FareVo fareVo = new FareVo();

        R info = memberFeignService.info(attrId);
        MemberAddressVo data = info.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {});
        if (data!=null){
            String phone = data.getPhone();
            String substring = phone.substring(phone.length() - 1, phone.length());
            BigDecimal bigDecimal = new BigDecimal(substring);
            fareVo.setAddress(data);
            fareVo.setFare(bigDecimal);

            return fareVo;
        }
        return null;
    }

}